<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      /*
      二叉树的最大深度有两种办法解决：
      ①通过层序遍历，获取每一层，然后计算深度
      ②递归 获取每一个节点的深度，然后传入根节点即可
      */
      var maxDepth = function (root) {
        function getDepth(root) {
          if (!root) return 0
          return Math.max(getDepth(root.left), getDepth(root.right)) + 1
        }
        let res = getDepth(root)
        return res
      }
      /* 
      var maxDepth = function (root) {
        function levelOrder(root) {
          if (!root) return []
          let res = []
          let queue = [root]
          while (queue.length) {
            res.push([])
            let len = queue.length
            for (let i = 0; i < len; i++) {
              let node = queue.shift()
              res[res.length - 1].push(node.val)
              if (node.left) {
                queue.push(node.left)
              }
              if (node.right) {
                queue.push(node.right)
              }
            }
          }
          return res
        }
        let result = levelOrder(root)
        return result.length
      } */
    </script>
  </body>
</html>
